layui.use(['jquery','layer'],function(){
    var $ = layui.jquery;
    var layer = layui.layer;
    var zTree;
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "categroyId",
                pIdKey: "parentId",
                rootPId: -1
            },
            key: {
                name: "title"
            }
        },
        check:{
            enable: true
        },
        expandSpeed :"fast",

        view:{
            dblClickExpand :false
        },
        callback: {
            beforeCheck : function(treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                zTree.checkAllNodes(false);
                return true;
            }
        }
    };
    $(document).ready(function(){
        $.ajax({
            url: '/product/getProductTree',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                zTree = $.fn.zTree.init($("#demoTree"), setting, data.data);
                zTree.expandAll(true);
                if($('input[name = categroyId]',window.parent.document).val()){
                    var node = zTree .getNodeByTId("tree_"+$('input[name = categroyId]',window.parent.document).val());
                    zTree.checkNode(node, true, true);
                }
            }
        })
    });

    //取消
    $('#cancel').on('click',function () {
        var player = parent.layer;
        player.close(player.index);
    });
    //确定
    $('#define').on('click',function () {
        var zTreeObj = $.fn.zTree.getZTreeObj('demoTree');
        var nodes = zTreeObj.getSelectedNodes(true);
        var classify = nodes[0].title;
        var categroyId = nodes[0].categroyId;
        parent.$('input[name=categroyName]').val(classify);
        parent.$('input[name=categroyId]').val(categroyId);
        var player = parent.layer;
        player.close(player.index);
    })
})